package primitives;

import java.util.ArrayList;

import utility.Intersection;
import utility.Vector3;

/*****************************
 *     3----------2
 *    /          /|
 *   /          / |
 *  /          /  |
 * 0----------1   |
 * |          |   |6
 * |          |  /
 * |          | /
 * |          |/
 * 4----------5
 * 
 *****************************/
public class Box extends Shape {
	
	public boolean biggestBox = false;
	
	public double reflectance = 0.65;
	public double emission = 0;

	public static final Vector3[] vec3 = new Vector3[]{
		new Vector3(new double[]{-0.5,  0.5,  0.5}),
		new Vector3(new double[]{ 0.5,  0.5,  0.5}),
		new Vector3(new double[]{ 0.5,  0.5, -0.5}),
		new Vector3(new double[]{-0.5,  0.5, -0.5}),
		new Vector3(new double[]{-0.5, -0.5,  0.5}),
		new Vector3(new double[]{ 0.5, -0.5,  0.5}),
		new Vector3(new double[]{ 0.5, -0.5, -0.5}),
		new Vector3(new double[]{-0.5, -0.5, -0.5})
	};
	
	public void initPatches() {
		patches = new ArrayList<Patch>(12);
		patches.add(new Patch(new Vector3[]{vec3[0], vec3[4], vec3[1]}, this));
		patches.add(new Patch(new Vector3[]{vec3[5], vec3[1], vec3[4]}, this));
		patches.add(new Patch(new Vector3[]{vec3[1], vec3[5], vec3[2]}, this));
		patches.add(new Patch(new Vector3[]{vec3[6], vec3[2], vec3[5]}, this));
		patches.add(new Patch(new Vector3[]{vec3[2], vec3[6], vec3[3]}, this));
		patches.add(new Patch(new Vector3[]{vec3[7], vec3[3], vec3[6]}, this));
		patches.add(new Patch(new Vector3[]{vec3[3], vec3[7], vec3[0]}, this));
		patches.add(new Patch(new Vector3[]{vec3[4], vec3[0], vec3[7]}, this));
		patches.add(new Patch(new Vector3[]{vec3[3], vec3[0], vec3[2]}, this));
		patches.add(new Patch(new Vector3[]{vec3[1], vec3[2], vec3[0]}, this));
		patches.add(new Patch(new Vector3[]{vec3[4], vec3[7], vec3[5]}, this));
		patches.add(new Patch(new Vector3[]{vec3[6], vec3[5], vec3[7]}, this));
	}

	public void initLight() {
		for(Patch patch : patches) {
		//	int patchColor = (color == -1) ? new Random().nextInt() : color;
		//	patches.get(i).color = patchColor;
		//	patches.get(i+1).color = patchColor;
			patch.emission = emission;
			patch.reflectance = reflectance;
		}
	}
	
	@Override
	public Intersection<Patch,Double> intersects(Vector3 eyePos, Vector3 pixel){
		Intersection<Patch,Double> closestIntersection = 
				new Intersection<Patch,Double>(null, Double.POSITIVE_INFINITY);
		for(Patch patch : patches) {
			Intersection<Patch,Double> intersection = 
					patch.intersects(worldToLocal.multiply(eyePos), 
					worldToLocal.multiply(pixel));
			if(!Double.isNaN(intersection.t) && 
					intersection.t < closestIntersection.t && 
					intersection.t > 0.0) {
				closestIntersection = intersection;
			}
		}
		if(Double.isInfinite(closestIntersection.t)) {
			return new Intersection<Patch, Double>(null, Double.NaN);
		} else {
			return closestIntersection;
		}
	}

	public void printVectors() {
		for(int i = 0; i < 8; i++) {
			System.out.println("(" + vec3[i].x() + "," + 
					vec3[i].y() + "," +
					vec3[i].z() + ")");	
		}
	}
	
}
